﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LLClient.ClientServiceRef;
using LLClient.ServerProxy;

namespace LLClient.Portal
{
    /// <summary>
    /// Interaction logic for MainMenu.xaml
    /// </summary>
    public partial class MainMenu : Window
    {
        private Proxy _proxy = Proxy.GetInstance();
        private bool _eventRegistered = false;
        private Client _currentClient = Proxy.GetInstance().CurrentClient;

        public Client CurrentClient
        {
            get { return _currentClient; }
            set { _currentClient = value; }
        }

        public MainMenu()
        {
            InitializeComponent();
            this.Closing += WindowClosed;
            this.Title = "Main Menu - " + chatControl1.CurrentClient.Username;

            if (!_eventRegistered)
            {
                if (_proxy != null)
                    _proxy.ProxyCallbackEvent += _proxy_ProxyCallbackEvent;

                _eventRegistered = true;
            }
        }

        void _proxy_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (e.Sender != null)
                {
                    switch (e.CallbackType)
                    {
                        case CallbackType.BattleCreation:
                            new Battle.PlacementShipWindow().Show();
                            this.Close();
                            break;
                    }
                }
            }));
        }

        public void WindowClosed(object sender, EventArgs e)
        {
            chatControl1.windowClose(sender, e);
            challangeControl1.windowClose(sender, e);
            statsControl1.windowClose(sender, e);
            if (_proxy != null)
                _proxy.ProxyCallbackEvent -= _proxy_ProxyCallbackEvent;
            _eventRegistered = false;
        }
    }
}
